import { ListNode } from "./ListNode";

function reverseList(head: ListNode | null): ListNode | null {
  if (head === null || head.next === null) return head;

  // 递归到链表末尾，newHead会是原链表的最后一个节点
  const newHead = reverseList(head.next);

  // 将当前节点的下一个节点的next指向自己（反转）
  head.next.next = head;
  // 断开当前节点的原指向（防止循环）
  head.next = null;

  return newHead;
}

const node1 = new ListNode(1);
node1.next = new ListNode(2);
node1.next.next = new ListNode(3);

const newHead = reverseList(node1);
let current: ListNode | null = newHead;
while (current) {
  console.log(current.val);
  current = current.next;
}

export {};
